<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :title="title"
    width="600px">
    <dynForm
      ref="dataForm"
      :info-form="searchForm"
      :search-tags="searchTags"
      :select-list="selectList"
      :rules="rules"/>
    <span slot="footer">
      <el-button icon="el-icon-circle-close" @click="handleClose">{{
        $t("cancel")
      }}</el-button>
      <el-button
        v-btnThrottle="doSave"
        type="primary"
        icon="el-icon-receiving"

      >{{ $t('save') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { doAdd, doUpdate, getInfo } from '@/api/Tooling/toolingType'
export default {
  name: 'ToolingTypeForm',
  data() {
    return {
      dialogVisible: false,
      title: '',
      searchForm: {},
      searchTags: [
        { label: this.$t('toolingType'), prop: 'toolingType', tag: 'DynInput', disabled: false },
        { label: this.$t('mode'), prop: 'mode', tag: 'DynSelect' },
        { label: this.$t('category'), prop: 'category', tag: 'DynSelect' },
        { label: this.$t('lifeControl'), prop: 'lifeTime', tag: 'DynSelect' },
        { label: this.$t('description'), prop: 'description', tag: 'DynTextarea', row: 3, show_word_limit: true, maxlength: 200 }
      ],
      selectList: Object.freeze({
        mode: [{ id: 1, name: this.$t('machineMode') }, { id: 2, name: this.$t('inventoryModel') }],
        category: [{ id: 1, name: '硬件' }, { id: 2, name: '备件' }, { id: 3, name: 'Dummy' }],
        lifeTime: [{ id: 0, name: this.$t('unlifeControl') }, { id: 1, name: this.$t('lifeControl') }]
      }),
      rules: {
        toolingType: [{ required: true, message: `${this.$t('pleaseInput')}${this.$t('toolingType')}`, trigger: 'blur' }],
        mode: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('mode')}`, trigger: 'change' }],
        category: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('category')}`, trigger: 'change' }],
        lifeTime: [{ required: true, message: `${this.$t('pleaseSelect')}${this.$t('lifeControl')}`, trigger: 'change' }]
      }
    }
  },
  methods: {
    doSave() {
      this.$refs['dataForm'].$refs['dynForm'].validate((valid) => {
        if (valid) {
          if (this.searchForm.id) {
            doUpdate(this.$clearNull(this.searchForm)).then(res => {
              this.$betterTips(res, this.handleClose, this.handleUpdate)
            })
          } else {
            doAdd(this.$clearNull(this.searchForm)).then(res => {
              this.$betterTips(res, this.handleClose, this.handleUpdate)
            })
          }
        }
      })
    },
    handleOpen(row) {
      if (row && row.id) {
        getInfo({ id: row.id }).then(res => {
          this.$set(this.searchTags, 0, { label: this.$t('toolingType'), prop: 'toolingType', tag: 'DynInput', disabled: true })
          this.searchForm = res.data
          this.title = this.$t('modify')
        })
      } else {
        this.title = this.$t('add')
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].$refs['dynForm'].clearValidate()
      })
    },
    handleUpdate() {
      this.$emit('initList')
    },
    handleClose() {
      Object.assign(this.$data, this.$options.data.call(this))
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-form {
    .el-form-item {
        width: 100%;
        // 感觉有点问题
        .el-form-item__content{
            width: 65%;
            .el-select {
                width: 100%;
            }
        }
    }
}
</style>
